package com.zoo.diannaopeijian.service.impl;

import com.zoo.diannaopeijian.comm.page.Page;
import com.zoo.diannaopeijian.comm.sql.GeneratorExampleBuilder;
import com.zoo.diannaopeijian.domain.Stock;
import com.zoo.diannaopeijian.domain.StockExample;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.domain.Warehouse;
import com.zoo.diannaopeijian.mapper.StockMapper;
import com.zoo.diannaopeijian.service.StockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.List;
import java.util.Map;

/**
 * Created by zx583 on 2018-01-27.
 */
@Service
public class StockServiceImpl implements StockService {
    @Resource
    private StockMapper stockMapper;

    @Override
    public boolean insertStock(Stock stock) {
        return stockMapper.insertSelective(stock) == 1;
    }

    @Override
    public boolean addStock(Stock stock) {
        Integer num = stock.getStocknum();

        stock.setStocknum(null);
        StockExample example = (StockExample) GeneratorExampleBuilder.buildEqualExample(StockExample.class, stock, false);

        if (example == null) {
            throw new RuntimeException("非法的库存查询对象");
        }

        List<Stock> oldStocks = stockMapper.selectByExample(example);

        if (oldStocks == null || oldStocks.size() == 0) {
            stock.setStocknum(num);
            return insertStock(stock);
        }

        if (oldStocks.size() != 1) {
            return false;
        }

        return stockMapper.updateStockNum(num, oldStocks.get(0).getStockid()) == 1;
    }


    @Override
    public boolean reduceStock(Stock stock) {
        Integer num = stock.getStocknum();

        stock.setStocknum(null);
        StockExample example = (StockExample) GeneratorExampleBuilder.buildEqualExample(StockExample.class, stock, false);
        if (example == null) {
            // 非法的库存对象
            return true;
        }
        List<Stock> oldStocks = stockMapper.selectByExample(example);

        if (oldStocks == null || oldStocks.size() == 0) {
            return false;
        }

        if (oldStocks.size() != 1) {
            return false;
        }

        if (oldStocks.get(0).getStocknum() < num) {
            return false;
        }

        return stockMapper.updateStockNum(-num, oldStocks.get(0).getStockid()) == 1;
    }

    @Override
    public boolean reduceStocks(List<Stock> stocks) {
        for (Stock stock : stocks) {
            if (!reduceStock(stock)) {
                return false;
            }
        }

        return true;
    }

    @Transient
    @Override
    public boolean addStocks(List<Stock> stocks) {
        for (Stock stock : stocks) {
            if (!addStock(stock)) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean updateStock(Stock stock) {
        return stockMapper.updateByPrimaryKeySelective(stock) == 1;
    }

    @Override
    public List<Map> getWareStock(Integer componentid, Integer componentparameterdetailid, Integer warehouseid, Ware ware) {
        return stockMapper.getWareStock(componentid, componentparameterdetailid, warehouseid, ware);
    }

    @Override
    public List<Map> getWareStockWithPage(Page page, Integer componentid, Integer componentparameterdetailid, Integer warehouseid, Ware ware) {
        page.onPage();

        return getWareStock(componentid, componentparameterdetailid, warehouseid, ware);
    }

    @Override
    public List<Map> getFuzzyWareStock(Integer componentid, Integer componentparameterdetailid, Integer warehouseid, Ware ware) {
        return stockMapper.getFuzzyWareStock(componentid, componentparameterdetailid, warehouseid, ware);
    }

    @Override
    public List<Map> getFuzzyStockWithPage(Page page, Integer componentid, Integer componentparameterdetailid, Integer warehouseid, Ware ware) {
        page.onPage();

        return getFuzzyWareStock(componentid, componentparameterdetailid, warehouseid, ware);
    }

    @Override
    public Integer getStockNum(Stock stock) {
        StockExample example = (StockExample) GeneratorExampleBuilder.buildEqualExample(StockExample.class, stock, false);

        if (example == null) {
            return null;
        }

        List<Stock> stocks = stockMapper.selectByExample(example);

        if (stocks == null || stocks.size() != 1) {
            return null;
        }

        return stocks.get(0).getStocknum();
    }
}
